package com.hwadeefsc.springboot.service.impl;

import com.hwadeefsc.springboot.dao.GradesDao;
import com.hwadeefsc.springboot.entity.Grades;
import com.hwadeefsc.springboot.service.GradesService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Grades)表服务实现类
 *
 * @author makejava
 * @since 2024-07-09 21:43:27
 */
@Service("gradesService")
public class GradesServiceImpl implements GradesService {
    @Resource
    private GradesDao gradesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param gradeId 主键
     * @return 实例对象
     */
    @Override
    public Grades queryById(Integer gradeId) {
        return this.gradesDao.queryById(gradeId);
    }

    /**
     * 分页查询
     *
     * @param grades 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Grades> queryByPage(Grades grades, PageRequest pageRequest) {
        long total = this.gradesDao.count(grades);
        return new PageImpl<>(this.gradesDao.queryAllByLimit(grades, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param grades 实例对象
     * @return 实例对象
     */
    @Override
    public Grades insert(Grades grades) {
        this.gradesDao.insert(grades);
        return grades;
    }

    /**
     * 修改数据
     *
     * @param grades 实例对象
     * @return 实例对象
     */
    @Override
    public Grades update(Grades grades) {
        this.gradesDao.update(grades);
        return this.queryById(grades.getGradeId());
    }

    /**
     * 通过主键删除数据
     *
     * @param gradeId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer gradeId) {
        return this.gradesDao.deleteById(gradeId) > 0;
    }


    /**
     * 通过studentId查询数据
     *
     * @param studentId 主键
     * @return 是否成功
     */
    @Override
    public List<Grades> queryByStudentId(Integer studentId) {
        return this.gradesDao.queryByStudentId(studentId);
    }

    @Override
    public List<Grades> queryAllData() {
        return this.gradesDao.getALLGradeData();
    }
}
